package com.leoman.spinningmachine.tab3;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.adapter.BaseRecyclerAdapter;
import com.leoman.helper.listener.OnDlg1ParamListener;
import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.DataUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.InvitationBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 访客邀请Activity
 */

public class InvitationActivity extends BaseActivity {

    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private InvitationAdapter adapter;
    private List<InvitationBean> list = new ArrayList<>();
    private int num = 15;
    private String time;    //当前时间
    private String astrict; //限制时间
    private String mac;
    private String token;
    private String address;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_invitation;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        AppManager.getInstance().addActivity(this);
        initTitle("访客邀请");

        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().opendoor(this, 1);

        tvTime.setText(num + "分钟");
        setLayoutManager(recyclerView, LinearLayoutManager.VERTICAL, true);
        adapter = new InvitationAdapter(R.layout.item_invitation, list);
        recyclerView.setAdapter(adapter);

        adapter.setClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                list.get(position).setSelect(!list.get(position).isSelect());
                adapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.iv_add, R.id.iv_reduce, R.id.tv_commit, R.id.ll_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                onBackPressed();
                break;
            case R.id.iv_add:
                if (num < 60) {
                    num++;
                    tvTime.setText(num + "分钟");
                }
                break;
            case R.id.iv_reduce:
                if (num > 15) {
                    num--;
                    tvTime.setText(num + "分钟");
                }
                break;
            case R.id.tv_commit:
                long m = System.currentTimeMillis();
                time = DataUtil.TimeStamp3time(m);

                astrict = DataUtil.TimeStamp3time(m + (num * 60 * 1000));

                mac = "";
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isSelect()) {
                        mac += TextUtils.isEmpty(mac) ? list.get(i).mac : "," + list.get(i).mac;
                    }
                }

                token = SharedPreferencesUtils.getInstance().getString(Constant.TOKEN);
                if (!TextUtils.isEmpty(mac)) {
                    address = "http://erfangji.natapp1.cc/api/api/" + "toClick?token=" + token + "&time=" + time + "&mac=" + mac + "&astrict=" + astrict;

//                    String durl = Base64.encodeToString(address.getBytes(), Base64.DEFAULT);  //Base64加密
//                    Log.e("-------加密-------", durl);
//                    String str2 = new String(Base64.decode(durl.getBytes(), Base64.DEFAULT)); //Base64解密
//                    Log.e("--------解码-------", str2);

                    DialogUtils.getInstance().show(this);
                    WebServiceApi.getInstance().dUrl(address, this, 2);

                } else {
                    ToastUtil.showToast(this, "请授权", Toast.LENGTH_SHORT);
                }
                break;
        }
    }

    //复制方法
    private void Copy(String content) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", content);
        cm.setPrimaryClip(mClipData);
        ToastUtil.showToast(this, "复制成功，请到微信、QQ等应用粘贴", Toast.LENGTH_SHORT);
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse, tag);
        DialogUtils.getInstance().cancel();
        if (tag == 1) {
            List<InvitationBean> data = apiResponse.data.list;
            if (data.size() != 0) {
                for (int i = 0; i < data.size(); i++) {
                    InvitationBean bean = new InvitationBean();
                    bean.name = data.get(i).name;
                    bean.mac = data.get(i).mac;
                    list.add(bean);
                }
                adapter.notifyDataSetChanged();
            } else {
                ToastUtil.showToast(this, "暂无权限，请联系后台管理人员添加", Toast.LENGTH_SHORT);
            }

        } else if (tag == 2) {
            final String url = apiResponse.data.url;
            //弹出提示框
            DialogUtils.getInstance().share(this, new OnDlg1ParamListener() {
                @Override
                public void click(String text) {
                    Copy(url);
                }
            });
        }
    }
}
